<?php
namespace app\admin\controller;
use think\Db;
use think\Controller;
use app\common\controller\AdminBaseController;

class Sys extends AdminBaseController
{
	//更新日志
    public function iteration_list()
    {
    	$data=Db::name('sys_iteration')->select();
    	$this->assign('data',$data);
    	return $this->fetch();
    }
	//商家提现记录
    public function txlist(){
    	
    	if(!empty(input('begin_time')) && !empty(input('end_time'))){
			$where['a.create_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
    	$where['a.user_type'] = 1;
    	$where['a.cash_state'] = ['in',[2,3]];
    	$list=Db::table('user_withdrawals')
    			->alias('a')
    			->field('a.id,b.mobile,b.agent_name,a.user_id,a.user_type,a.cash_state,a.create_time,b.balance,a.balance as tixianmoney,a.actual_balance,a.bank_no,a.bank_account,a.bank_username,a.bank_mobile')
    			->join('info_agent b','a.user_id = b.id')
    			->where($where)
    			->order('create_time desc')
    			->paginate(15,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
		return $this->fetch();
    }
    
    //商家提现记录导出
    public function txlist_dc(){
    	if(!empty(input('begin_time')) && !empty(input('end_time'))){
			$where['a.create_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
    	$where['a.user_type'] = 1;
    	$where['a.cash_state'] = ['in',[2,3]];
    	
		$list=Db::table('user_withdrawals')
    			->alias('a')
    			->field('a.id,b.mobile,b.agent_name,a.user_id,a.user_type,a.cash_state,a.create_time,b.balance,a.balance as tixianmoney,a.actual_balance,a.bank_no,a.bank_account,a.bank_username,a.bank_mobile')
    			->join('info_agent b','a.user_id = b.id')
    			->where($where)
    			->order('create_time desc')
    			->select();
    			
		$str ="商户名称,商户账号,商户余额,提现金额,实际到帐金额,提现账户,开户行支行,户名,银行预留手机号,申请时间,状态"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
			$agent_name = iconv('utf-8','gbk',$row['agent_name']); 
			$mobile = iconv('utf-8','gbk',$row['mobile']);
			$balance = iconv('utf-8','gbk',abs($row['balance']));
			$tixianmoney = iconv('utf-8','gbk',abs($row['tixianmoney']));
			$actual_balance = iconv('utf-8','gbk',abs($row['actual_balance']));
			$bank_no = iconv('utf-8','gbk',$row['bank_no']);
			$bank_account = iconv('utf-8','gbk',$row['bank_account']);
			$bank_username = iconv('utf-8','gbk',$row['bank_username']);
			$bank_mobile = iconv('utf-8','gbk',$row['bank_mobile']); 
			$create_time = iconv('utf-8','gbk',$row['create_time']);
			if($row['cash_state'] == 1){
				$cash_state = iconv('utf-8','gbk','处理中');
			}else if($row['cash_state'] == 2){
				$cash_state = iconv('utf-8','gbk','已完成');
			}
			$str .= $agent_name.",".$mobile.",".$balance.",".$tixianmoney.",".$actual_balance.",".$bank_no.",".$bank_account.",".$bank_username.",".$bank_mobile.",".$create_time.",".$cash_state.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='商家提现记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
    
    
	//用户提现记录
    public function mem_txlist(){
    	if(!empty(input('begin_time')) && !empty(input('end_time'))){
			$where['a.create_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
    	if(!empty(input('mobile'))){
			$where['b.mobile'] = input('mobile');
		}		
		
		$where['a.user_type'] = 2;
		$where['a.cash_state'] = ['in',[2,3]];
    	$list=Db::table('user_withdrawals')
    			->alias('a')
    			->field('a.id,b.mobile,a.user_id,a.user_type,a.cash_state,a.create_time,b.balance,a.balance as tixianmoney,a.actual_balance,a.bank_no,a.bank_account,a.bank_username,a.bank_mobile')
    			->join('info_user b','a.user_id = b.id')
    			->where($where)
    			->order('create_time desc')
    			->paginate(20,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
		return $this->fetch();
    }
    
    //用户提现记录
    public function mem_txlist_dc(){
    	if(!empty(input('begin_time')) && !empty(input('end_time'))){
			$where['a.create_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		$where['a.user_type'] = 2;
		$where['a.cash_state'] = ['in',[2,3]];
    	$list=Db::table('user_withdrawals')
    			->alias('a')
    			->field('a.id,b.mobile,a.user_id,a.user_type,a.cash_state,a.create_time,b.balance,a.balance as tixianmoney,a.actual_balance,a.bank_no,a.bank_account,a.bank_username,a.bank_mobile')
    			->join('info_user b','a.user_id = b.id')
    			->where($where)
    			->order('create_time desc')
    			->select();
    	
    	$str ="用户账号,用户余额,提现金额,实际到帐金额,提现账户,开户行支行,户名,银行预留手机号,申请时间,状态"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
			$mobile = iconv('utf-8','gbk',$row['mobile']);
			$balance = iconv('utf-8','gbk',abs($row['balance']));
			$tixianmoney = iconv('utf-8','gbk',abs($row['tixianmoney']));
			$actual_balance = iconv('utf-8','gbk',abs($row['actual_balance']));
			$bank_no = iconv('utf-8','gbk',$row['bank_no']);
			$bank_account = iconv('utf-8','gbk',$row['bank_account']);
			$bank_username = iconv('utf-8','gbk',$row['bank_username']);
			$bank_mobile = iconv('utf-8','gbk',$row['bank_mobile']); 
			$create_time = iconv('utf-8','gbk',$row['create_time']);
			if($row['cash_state'] == 1){
				$cash_state = iconv('utf-8','gbk','处理中');
				
			}else if($row['cash_state'] == 2){
				$cash_state = iconv('utf-8','gbk','已完成');
			}
			$str .= $mobile.",".$balance.",".$tixianmoney.",".$actual_balance.",".$bank_no.",".$bank_account.",".$bank_username.",".$bank_mobile.",".$create_time.",".$cash_state.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='用户提现记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
    }
    
    
    
	//oem提现记录
    public function oem_txlist(){
    	if(!empty(input('begin_time')) && !empty(input('end_time'))){
			$where['a.create_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		$where['a.cash_state'] = ['in',[2,3]];
    	$list=Db::table('oem_withdrawals')
    			->alias('a')
    			->field('a.id,b.mobile,b.user_name,a.cash_state,a.create_time,b.balance,a.balance as tixianmoney,a.actual_balance,a.bank_no,a.bank_account,a.bank_username,a.bank_mobile')
    			->join('info_oem b','a.oem_id = b.id')
				->where($where)
    			->order('create_time desc')
    			->paginate(20);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
    
    //oem提现记录
    public function oem_txlist_dc(){
    	if(!empty(input('begin_time')) && !empty(input('end_time'))){
			$where['a.create_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		$where['a.cash_state'] = ['in',[2,3]];
		$list=Db::table('oem_withdrawals')
    			->alias('a')
    			->field('a.id,b.mobile,b.user_name,a.cash_state,a.create_time,b.balance,a.balance as tixianmoney,a.actual_balance,a.bank_no,a.bank_account,a.bank_username,a.bank_mobile')
    			->join('info_oem b','a.oem_id = b.id')
				->where($where)
    			->order('create_time desc')
    			->select();
    	
    	$str ="OEM名称,OEM账号,OEM余额,提现金额,实际到帐金额,提现账户,开户行支行,户名,银行预留手机号,申请时间,状态"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$user_name = iconv('utf-8','gbk',$row['user_name']);
			$mobile = iconv('utf-8','gbk',$row['mobile']);
			$balance = iconv('utf-8','gbk',abs($row['balance']));
			$tixianmoney = iconv('utf-8','gbk',abs($row['tixianmoney']));
			$actual_balance = iconv('utf-8','gbk',abs($row['actual_balance']));
			$bank_no = iconv('utf-8','gbk',$row['bank_no']);
			$bank_account = iconv('utf-8','gbk',$row['bank_account']);
			$bank_username = iconv('utf-8','gbk',$row['bank_username']);
			$bank_mobile = iconv('utf-8','gbk',$row['bank_mobile']); 
			$create_time = iconv('utf-8','gbk',$row['create_time']);
			if($row['cash_state'] == 1){
				$cash_state = iconv('utf-8','gbk','处理中');
				
			}else if($row['cash_state'] == 2){
				$cash_state = iconv('utf-8','gbk','已完成');
			}
			$str .= $user_name.",".$mobile.",".$balance.",".$tixianmoney.",".$actual_balance.",".$bank_no.",".$bank_account.",".$bank_username.",".$bank_mobile.",".$create_time.",".$cash_state.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='oem提现记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
    }
	
	//佣金转余额
	public function yj_yue(){
    	$list=Db::table('log_oem_commission')
    			->alias('a')
    			->field('a.id,a.order_type,a.balance,a.message,a.oem_id,a.add_time')   			
    			->where('a.order_type',5)
    			->order('a.add_time desc')->paginate(20);
		
		$sum=Db::table('log_oem_commission')->where('order_type',5)->sum('balance');
    	$this->assign('list',$list);
		$this->assign('sums',$sum);
    	return $this->fetch();
	}

	//明细导出
    public function yue_dc(){
		$where['a.order_type'] = 5;
    	$list=Db::table('log_oem_commission')
    			->alias('a')
    			->field('a.id,a.order_type,a.balance,a.message,a.oem_id,a.add_time')   			
    			->where($where)
    			->order('a.add_time desc')
				->select();

		$str ="信息,金额,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
			$message = iconv('utf-8','gbk',$row['message']); 
			$balance = iconv('utf-8','gbk',$row['balance']); 
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $message.",".$balance.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='佣金转余额记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
    public function iteration_add(){
		if($_POST){
            $data['title']=$_POST['title'];
            $data['content']=$_POST['content'];
            $data['add_time']=now_time();
            $res=Db::name('sys_iteration')->insert($data);//分组数据库
            if($res){
               return	toSuccess('添加成功');
            } else {
               return toError(408,'添加失败');
            }
        }
    	
        return  $this->fetch();
    }
	//鼓励金兑现记录
    public function glj_ex(){
    	$list=db("log_user_score")->alias('u')->
    	join("info_user i","i.id=u.u_id")->
    	field("u.*,i.mobile")->where('u.order_type',9)->order("u.add_time desc")->paginate(15);
		$this->assign('list',$list);
		
		$sum = db("log_user_score")->field("sum(balance) as sum,sum(balance*0.95/10) as sumd,sum(balance*0.05/10) as sumf")->where('order_type',9)->find();
		$this->assign('sum',$sum);
		return  $this->fetch();
    }
	
	//鼓励金兑换明细导出
    public function gljex_dc(){
		$where['u.order_type'] = 9;
    	$list=db('log_user_score')
    			->alias('u')
				->join("info_user i","i.id=u.u_id")
    			->field('u.*,i.mobile')   			
    			->where($where)
    			->order('u.add_time desc')
				->select();
		$str ="会员,兑现鼓励金,到账金额,服务费,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
			$mobile = iconv('utf-8','gbk',$row['mobile'].'	'); 
			$balance = iconv('utf-8','gbk',$row['balance']);
			$balance1 = iconv('utf-8','gbk',abs($row['balance'])*0.95/10);
			$balance2 = iconv('utf-8','gbk',abs($row['balance'])*0.05/10); 
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $mobile.",".$balance.",".$balance1.",".$balance2.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='会员兑换记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//商家鼓励金兑现记录
	public function agent_ex(){
		$list=db("log_agent_score")->alias('u')->
    	join("info_agent i","i.id=u.agent_id")->
    	field("u.*,i.mobile,i.agent_name")->order("u.add_time desc")->where('u.order_type',6)->paginate(15);
		$this->assign('list',$list);
		
		$sum = db("log_agent_score")->field("sum(balance) as sum,sum(balance*0.95/10) as sumd,sum(balance*0.05/10) as sumf")->where('order_type',6)->find();
		$this->assign('sum',$sum);
		return  $this->fetch();
	}
	
	//商家鼓励金兑换明细导出
    public function agentex_dc(){
		$where['u.order_type'] = 6;
    	$list=db('log_agent_score')
    			->alias('u')
				->join("info_agent i","i.id=u.agent_id")
    			->field('u.*,i.mobile,i.agent_name')   			
    			->where($where)
    			->order('u.add_time desc')
				->select();
		$str ="商家,手机号,兑现鼓励金,到账金额,服务费,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$agent_name = iconv('utf-8','gbk',$row['agent_name']); 
			$mobile = iconv('utf-8','gbk',$row['mobile'].'	'); 
			$balance = iconv('utf-8','gbk',$row['balance']);
			$balance1 = iconv('utf-8','gbk',abs($row['balance'])*0.95/10);
			$balance2 = iconv('utf-8','gbk',abs($row['balance'])*0.05/10); 
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $agent_name.",".$mobile.",".$balance.",".$balance1.",".$balance2.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='商家兑换记录.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//代理兑现鼓励金
	public function oem_ex(){
		$list=db("log_oem_score")->alias('u')->
    	join("info_oem i","i.id=u.oem_id")->
    	field("u.*,i.user_name,i.mobile")->where('u.order_type',2)
    	->order("u.add_time desc")->paginate(15);
		$this->assign('list',$list);
		
		$sum = db("log_oem_score")->field("sum(balance) as sum,sum(balance*0.95/10) as sumd,sum(balance*0.05/10) as sumf")->where('order_type',2)->find();
		$this->assign('sum',$sum);
		return  $this->fetch();
	}
	
	//代理鼓励金兑换明细导出
    public function oemex_dc(){
		$where['u.order_type'] = 2;
    	$list=db('log_oem_score')
    			->alias('u')
				->join("info_oem i","i.id=u.oem_id")
    			->field("u.*,i.user_name,i.mobile") 			
    			->where($where)
    			->order('u.add_time desc')
				->select();
		$str ="代理名称,手机号,兑现鼓励金,到账金额,服务费,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$user_name = iconv('utf-8','gbk',$row['user_name']); 
			$mobile = iconv('utf-8','gbk',$row['mobile'].'	'); 
			$balance = iconv('utf-8','gbk',$row['balance']);
			$balance1 = iconv('utf-8','gbk',abs($row['balance'])*0.95/10);
			$balance2 = iconv('utf-8','gbk',abs($row['balance'])*0.05/10); 
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $user_name.",".$mobile.",".$balance.",".$balance1.",".$balance2.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='代理兑换记录.csv';   //设置文件名 
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//鼓励金购买记录
	public function glj_cz(){
		$list=db("log_user_score")->alias('u')->
    	join("info_user i","i.id=u.u_id")->
    	field("u.*,i.mobile")->where('u.order_type',7)
    	->order("u.add_time desc")->paginate(15);
		$this->assign('list',$list);
		
		$sum = db("log_user_score")->where('order_type',7)->sum('balance/4');
		$this->assign('sum',$sum);
		
		$sxsum = db("log_user_score")->where('order_type',7)->sum('balance/4*0.0025');
		$this->assign('sxsum',$sxsum);
		return  $this->fetch();
	}
	
	//鼓励金购买记录导出
    public function gljcz_dc(){
		$where['u.order_type'] = 7;
    	$list=db('log_user_score')
    			->alias('u')
				->join("info_user i","i.id=u.u_id")
    			->field("u.*,i.mobile") 			
    			->where($where)
    			->order('u.add_time desc')
				->select();
		$str ="用户编号,会员,购买金额,手续费,详细,到账鼓励金,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
			$u_id = iconv('utf-8','gbk',$row['u_id']);
			$mobile = iconv('utf-8','gbk',$row['mobile'].'	');
			$balance = iconv('utf-8','gbk',$row['balance']/4);
			$sxf = iconv('utf-8','gbk',$row['balance']/4*0.025);
			$message = iconv('utf-8','gbk',$row['message']);
			$balance1 = iconv('utf-8','gbk',$row['balance']); 
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $u_id.",".$mobile.",".$balance.",".$sxf.",".$message.",".$balance1.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='会员鼓励金购买记录.csv';   //设置文件名 
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//商家鼓励金购买记录
	public function agent_cz(){
		$list=db("log_agent_score")->alias('u')->
    	join("info_agent i","i.id=u.agent_id")->
    	field("u.*,i.mobile,i.agent_name")->order("u.add_time desc")->where('u.order_type',5)->paginate(15);
		$this->assign('list',$list);
		
		$sum = db("log_agent_score")->where('order_type',5)->sum('balance/10');
		$this->assign('sum',$sum);
		
		$sxsum = db("log_agent_score")->where('order_type',5)->sum('balance/10*0.025');
		$this->assign('sxsum',$sxsum);
		return  $this->fetch();
	}
	
	//商家鼓励金购买记录导出
    public function agentcz_dc(){
		$where['u.order_type'] = 5;
    	$list=db('log_agent_score')
    			->alias('u')
				->join("info_agent i","i.id=u.agent_id")
    			->field("u.*,i.mobile,i.agent_name")	
    			->where($where)
    			->order('u.add_time desc')
				->select();
		$str ="商家,手机号,购买金额,手续费,详细,到账鼓励金,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$agent_name = iconv('utf-8','gbk',$row['agent_name'].'	'); 
			$mobile = iconv('utf-8','gbk',$row['mobile'].'	'); 
			$balances = iconv('utf-8','gbk',$row['balance']/10);
			$sxf = iconv('utf-8','gbk',$row['balance']/10*0.0025);
			$msg = iconv('utf-8','gbk',$row['message']);
			$balance = iconv('utf-8','gbk',$row['balance']);
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $agent_name.",".$mobile.",".$balances.",".$sxf.",".$msg.",".$balance.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='商家鼓励金购买记录.csv';   //设置文件名 
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//代理鼓励金购买记录
	public function oem_cz(){
		$list=db("log_oem_score")->alias('u')->
    	join("info_oem i","i.id=u.oem_id")->
    	field("u.*,i.user_name,i.mobile")->where('u.order_type',5)
    	->order("u.add_time desc")->paginate(10);
		$this->assign('list',$list);
		return  $this->fetch();
	}
	
	//代理鼓励金购买记录导出
    public function oemcz_dc(){
		$where['u.order_type'] = 5;
    	$list=db('log_oem_score')
    			->alias('u')
				->join("info_oem i","i.id=u.oem_id")
    			->field("u.*,i.user_name,i.mobile") 			
    			->where($where)
    			->order('u.add_time desc')
				->select();
		$str ="代理名称,手机号,购买鼓励金,到帐鼓励金,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$user_name = iconv('utf-8','gbk',$row['user_name'].'	'); 
			$mobile = iconv('utf-8','gbk',$row['mobile'].'	'); 
			$balance = iconv('utf-8','gbk',$row['balance']);
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $user_name.",".$mobile.",".$balance.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='代理鼓励金购买记录.csv';   //设置文件名 
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//鼓励金转账记录
    public function glj_zz(){
    	$list=db("log_user_score")->alias('u')->
    	join("info_user i","i.id=u.zz_user_id")->
    	join("info_user f","f.id=u.sk_user_id")->
    	field("u.*,i.mobile as zzr,f.mobile as skr")->where('u.order_type',8)->order("u.add_time desc")->paginate(10);
		$this->assign('list',$list);
		return  $this->fetch();
    }
	
	//会员鼓励金转账记录
    public function gljzz_dc(){
		$where['u.order_type'] = 8;
    	$list=db('log_user_score')
    			->alias('u')
    			->join("info_user i","i.id=u.zz_user_id")
    			->join("info_user f","f.id=u.sk_user_id")
    			->field("u.*,i.mobile as zzr,f.mobile as skr") 			
    			->where($where)
    			->order('u.add_time desc')
				->select();
		$str ="转账人,收账人,转账鼓励金,服务费,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$zzr = iconv('utf-8','gbk',$row['zzr'].'	'); 
			$skr = iconv('utf-8','gbk',$row['skr'].'	'); 
			$balance = iconv('utf-8','gbk',$row['balance']);
			$balance1 = iconv('utf-8','gbk',abs($row['balance'])*0.05);
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $zzr.",".$skr.",".$balance.",".$balance1.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='会员鼓励金转账记录.csv';   //设置文件名 
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
	//经销商转账记录
	public function oem_zz(){
		$list=db("log_oem_score")->alias('u')->
		join("info_oem z","z.id=u.oem_id","left")->
    	join("info_user i","i.id=u.skr","left")->
    	join("info_agent f","f.id=u.skr","left")->
    	join("info_oem o","o.id=u.skr","left")->
    	field("u.*,z.mobile as zzr,f.mobile as skr,i.mobile as skr1,o.mobile as skr2")->where('u.order_type',3)->order("u.add_time desc")->paginate(10);
		$this->assign('list',$list);
		return  $this->fetch();
	}
	
	//经销商鼓励金转账记录
    public function oemzz_dc(){
		$where['u.order_type'] = 3;
    	$list=db('log_oem_score')
    			->alias('u')
				->join("info_oem z","z.id=u.oem_id","left")
    			->join("info_user i","i.id=u.skr","left")
    			->join("info_agent f","f.id=u.skr","left")
    			->join("info_oem o","o.id=u.skr","left")
    			->field("u.*,z.mobile as zzr,f.mobile as skr,i.mobile as skr1,o.mobile as skr2") 			
    			->where($where)
    			->order('u.add_time desc')
				->select();
		$str ="转账人,转账对象,收账人,转账鼓励金,服务费,交易时间"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$zzr = iconv('utf-8','gbk',$row['zzr'].'	');
			$typename = '';
			$skr = '';
			if($row['type']==1){
				$typename = iconv('utf-8','gbk','转省代');
				$skr = iconv('utf-8','gbk',$row['skr'].'	');
			}else if($row['type']==2){
				$typename = iconv('utf-8','gbk','转市代');
				$skr = iconv('utf-8','gbk',$row['skr'].'	');
			}else if($row['type']==3){
				$typename = iconv('utf-8','gbk','转商家');
				$skr = iconv('utf-8','gbk',$row['skr1'].'	');
			}else{
				$typename = iconv('utf-8','gbk','转会员');
				$skr = iconv('utf-8','gbk',$row['skr2'].'	');
			}
			$balance = iconv('utf-8','gbk',$row['balance']);
			$balance1 = iconv('utf-8','gbk',abs($row['balance'])*0.05);
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$str .= $zzr.",".$typename.",".$skr.",".$balance.",".$balance1.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='经销商鼓励金转账记录.csv';   //设置文件名 
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
	
    public function iteration_edit(){
		if($_POST){
	        $data['title']=$_POST['title'];
	        $data['content']=$_POST['content'];
	        $result=Db::name('sys_iteration')->where('id', $_POST['id'])->update($data);
	        if($result){
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
       $id=input('id');
       $data =Db::name('sys_iteration')->where('id', $id)->find();
       $this->assign('data',$data);
       return  $this->fetch();
    }
    
    public function iteration_del(){
       $result =Db::name('sys_iteration')->where('id', $_GET['id'])->delete();;
        if($result){
			 return json(['code'=>200,'msg'=>'成功']);
       }else{
       		return json(['code'=>402,'msg'=>'失败']);
       }
    	
        return  $this->fetch();
    }
    //使用帮助
    public function helps_list()
    {
    	
    	$helps = Db::name('sys_helps') ->order('add_time desc')->paginate(15);
    	$this->assign('data',$helps);
    	return $this->fetch();
    }
   
    public function helps_add(){
		if($_POST){
			$data['pid']=$_POST['pid'];
            $data['title']=$_POST['title'];
            $data['content']=$_POST['content'];
            $data['add_time']=now_time();
            $res=Db::name('sys_helps')->insert($data);//分组数据库
            if($res){
             return json(['code'=>200,'msg'=>'成功']);
            } else {
             return json(['code'=>402,'msg'=>'失败']);
            }
        }
    	$data=Db::name('sys_helps')->where('pid = 0')->select();
    	$this->assign('data',$data);
        return  $this->fetch();
    }
   
     public function helps_edit(){
		if($_POST){
    		$id=$_POST['id'];
    		$data['title']=$_POST['title'];	
	        $data['content']= $_POST['content'];
	        $result=Db::name('sys_helps')->where('id',$id)->update($data);
	        if($result){
				return json(['code'=>200,'msg'=>'成功']);
	       }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	       }
    	}
    	
    	$id=input('id');
        $data=Db::name('sys_helps')->where('id',$id)->find();
        $data['content'] = html_entity_decode($data['content']);
        $this->assign('data',$data);
    	
        return  $this->fetch();
    }
    public function helps_del(){
       $result =Db::name('sys_helps')->where('id', $_GET['id'])->delete();;
        if($result){
			 return json(['code'=>200,'msg'=>'成功']);
       }else{
       		return json(['code'=>402,'msg'=>'失败']);
       }
    	
        return  $this->fetch();
    }
    
    //意见反馈
    public function complain_list()
    {
    	$data=Db::name('sys_iteration')->select();
    	$this->assign('data',$data);
    	return $this->fetch();
    }
    public function complain_del(){
       $result =Db::name('sys_iteration')->where('id', $_GET['id'])->delete();;
        if($result){
			 return json(['code'=>200,'msg'=>'成功']);
       }else{
       		return json(['code'=>402,'msg'=>'失败']);
       }
    	
        return  $this->fetch();
    }
    
    //物流公司
    public function logistics_list()
    {
    	$data=Db::table('agent_logistics')->select();
    	$this->assign('data',$data);
    	return $this->fetch();
    }
    //oem资质审核
    public function user_shenhe()
    {
    	$list=Db::table('sys_member_verif')->alias('a')
		->join('info_oem b','a.user_id = b.id','LEFT')
		->field('a.id,a.user_type,a.user_id,a.add_time,b.user_name,b.mobile,b.is_verify')
		->where('a.user_type = 2')
		->order('a.add_time desc')->paginate(15);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
	//个人资质审核
    public function memuser_shenhe()
    {
        $list=Db::table('sys_member_verif')->alias('a')
		->join('info_user b','a.user_id = b.id','LEFT')
		->field('a.id,a.user_type,a.user_id,a.add_time,b.user_name,b.nickname,b.mobile,b.is_verify')
		->where('a.user_type = 1')
		->order('a.add_time desc')->paginate(15);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
    //商家官方资质审核
    public function agent_shenhe1()
    {
    	$list=Db::table('sys_agent_verif') ->order('add_time desc')->paginate(15);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
    //商家银行资质审核
    public function agent_shenhe2()
    {
    	if(input('name')){
			$where['a.agent_name'] = ['like','%'.input('name').'%'];
		}
		
		if(input('mobile') && input('mobile')>0){
			$where['a.mobile'] = ['like','%'.input('mobile').'%'];
		}
		
		if(input('state') && input('state')>0){		
			if(input('state')!=5){
				$where['b.state'] = input('state');
			}		
		}else{		
			$where['b.state'] = ['in','3,4'];
		}
		$where['a.status'] = 1;			
		$list=Db::table('info_agent')
    	->alias('a')
    	->join('agent_qianyue b','a.id = b.agent_id')
    	->field('b.agent_name1 as agent_name1,b.contacts as contacts,b.state as state,b.add_time as add_time,a.id as id,b.id as sid,a.mobile')
		->order('b.add_time desc')
		->where($where)
		->paginate(20,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
    //用户提现申请列表
    public function user_tixian()
    {
    	if(input('mobile') && input('mobile')>0){
			$where['b.mobile'] = ['like','%'.input('mobile').'%'];
		}
		$where['a.user_type'] = 2;
		$where['a.cash_state'] = 1;
		
		$sys_member_verif = db('sys_member_verif')->group('user_id')->buildSql();
    	$list=Db::table('user_withdrawals')
    			->alias('a')
    			->field('v.bank_name1,a.id,b.mobile,b.user_name,a.user_id,a.user_type,a.cash_state,a.create_time,b.balance,a.balance as tixianmoney,a.actual_balance,a.bank_no,a.bank_account,a.bank_username,a.bank_mobile')
    			->join('info_user b','a.user_id = b.id','left')
    			->join("$sys_member_verif v",'v.user_id= b.id',"left")
    			->where($where)
    			->order('create_time desc')
    			->paginate(20,false,['query'=>request()->param()]);
    	
    	$this->assign('list',$list);
    	$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    
     //商家提现申请列表
    public function agent_tixian()
    {
    	$list=Db::table('user_withdrawals')
    			->alias('a')
    			->field('a.id,b.mobile,b.agent_name,a.user_id,a.user_type,a.cash_state,a.create_time,b.balance,a.balance as tixianmoney,a.actual_balance,a.bank_no,a.bank_account,a.bank_username,a.bank_mobile')
    			->join('info_agent b','a.user_id = b.id')
    			->where('a.user_type',1)
				->where('a.cash_state',1)
    			->order('create_time desc')
    			->paginate(20,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
    	$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    
    //oem提现申请列表
    public function oem_tixian()
    {
    	$list=Db::table('oem_withdrawals')
    			->alias('a')
    			->field('a.id,b.mobile,b.user_name,a.cash_state,a.create_time,b.balance,a.balance as tixianmoney,a.actual_balance,a.bank_no,a.bank_account,a.bank_username,a.bank_mobile')
    			->join('info_oem b','a.oem_id = b.id')
				->where('a.cash_state',1)
    			->order('create_time desc')->paginate(20);
    	$this->assign('list',$list);
    	$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    	
    	
    	
    	$list=Db::table('oem_withdrawals') 
    			->order('create_time desc')->paginate(20);
    	$this->assign('list',$list);
    	$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    
	
	//商家资质审核
    public function agent_examine(){
       $id=input('id');
    	$data=Db::table('agent_qianyue')
    	->alias('a')
    	->join('sys_platform_type w','a.trade_type = w.id','left')
    	->field('a.*,w.type_name type_name')
    	->where('a.agent_id',$id)
    	->find();
		$this->assign('data',$data);
    	return $this->fetch();
    }
	
	//审核通过
	public function examine_pass(){
		$id=input('id');
		$type=input('type');
		if($type==1){		//商家资质审批通过
			$where['is_state']=1;
			$results=Db::name('info_agent')->where('id',$id)->update($where);
			db("agent_qianyue")->where("agent_id",$id)->update(["state"=>1]);
			$agent=db("info_agent")->find($id);
			$update['is_agent']=2;
			$update['tj_agent']=$id;
			db('info_user')->where('id',$agent['user_id'])->update($update);
			$store_data['agent_id']=$id;
			$store_data['store_name']=$agent['agent_name'];
			$store_data['addres']=$agent['address'];
			$store_data['add_time']=now_time();
			db('agent_store')->insert($store_data);
			update_level($agent['user_id'],$id);
		}else{
			//查询资料信息
			$verifinfo = Db::name('sys_member_verif')->find($id);
			//修改个人资料状态 1会员2OEM3省代4市代
			$datas['is_verify']=1;
			if($verifinfo['user_type']==1){			
				$results=Db::name('info_user')->where('id',$verifinfo['user_id'])->update($datas);
			}else{
				$results=Db::name('info_oem')->where('id',$verifinfo['user_id'])->update($datas);
			}
		}
        
        if($results){
			return json(['code'=>200,'msg'=>'成功']);
       }else{
       		return json(['code'=>402,'msg'=>'失败']);
       }
    	
    	return $this->fetch();
    }
	
	//审核驳回
	public function agent_reject(){
		$this->assign('id',input('id'));
		$this->assign('type',input('type'));
    	return $this->fetch();
    }
	
	//审核驳回
	public function examine_reject(){
		$id=input('id');
		$type=input('type');

		//驳回  1 银行资质  2 资料认证(个人、OEM)
		if($type==1){
			$where['is_state']=3;
			$result=Db::name('info_agent')->where('id',$id)->update($where);
			$update['state']=3;
			$update['reason']=input('content');
			$results=db("agent_qianyue")->where("agent_id",$id)->update($update);
		}else{
			//查询资料信息
			$verifinfo = Db::name('sys_member_verif')->find($id);
			//修改个人资料状态 1会员2OEM、省代、市代
			$data['is_verify']=3;			
			if($verifinfo['user_type']==1){			
				$result=Db::name('info_user')->where('id',$verifinfo['user_id'])->update($data);
			}else{
				$result=Db::name('info_oem')->where('id',$verifinfo['user_id'])->update($data);
			}
			
			//驳回原因
			$update['reason']=input('content');
			$results=db("sys_member_verif")->where("id",$id)->update($update);
		}
        
        if($results){
        	//短信开关 1 开  0 关
        	if($type == 1&&config('sms_flag')==1){       		
        		//获取商家信息
        		$agent_info = Db::name('info_agent')->where('id',$id)->find();
        		//获取商家服务商信息
        		$city_info = Db::name('info_oem')->where('id',$agent_info['agency_id2'])->find();
        		//发送短信提醒商家
	        	$tem_w['title']='审核失败';
				$tem=db("sms_template")->where($tem_w)->find();
				$msg=sendsms($tem['sign_name'],$agent_info['mobile'],$tem['tid'],'code','商家',config('cut_oem_id'));
	        	
	        	//发送短信提醒服务商
	        	$tem_w['title']='审核失败';
				$tem=db("sms_template")->where($tem_w)->find();
				$msg=sendsms($tem['sign_name'],$city_info['mobile'],$tem['tid'],'code',$city_info['user_name']."：".$agent_info['mobile']."签约资料未审核通过",config('cut_oem_id'));
        	}
			
			return json(['code'=>200,'msg'=>'成功']);
       }else{
       		return json(['code'=>402,'msg'=>'失败']);
       }
    	
    	$list=Db::table('agent_qianyue') ->order('add_time desc')->paginate(5);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
	
	//个人资质审核
    public function user_examine(){
       	$id=input('id');
    	$data=Db::table('sys_member_verif')->alias('a')
		->join('info_user b','a.user_id = b.id','LEFT')
		->field('a.*,b.is_verify')
    	->where('a.id',$id)->find();
		$this->assign('data',$data);
    	return $this->fetch();
    }

	//提现审核
	public function tx_operate(){
		$this->assign('id',input('id'));
		$this->assign('type',input('type'));
		$this->assign('usertype',input('usertype'));
		$result=Db::name('user_withdrawals')->where('id',input('id'))->find();
		$this->assign('list',$result);
    	return $this->fetch();
    }
    
    //oem提现审核
	public function oem_tx_operate(){
		$this->assign('id',input('id'));
		$this->assign('type',input('type'));
		$result=Db::name('oem_withdrawals')->where('id',input('id'))->find();
		$this->assign('list',$result);
    	return $this->fetch();
    }
	
	//会员，商家提现审核结果
	public function user_tx_result(){
		$id			= input('id');			//提现记录id
		$type		= input('type');		//操作，2通过；3驳回	
		$usertype	= input('usertype');	//用户类型
		$thumb		= input('thumb');		//凭证截图
		$content	= input('content');		//内容
		
		//获取提现记录
		$user_withdrawals_info = Db::name('user_withdrawals')->where('id',input('id'))->find();
		
		if($type==2){	//审核通过
			$cut_withdrawals['cash_state'] = 2;
			//检查提现用户类型
			if($user_withdrawals_info['user_type'] == 1 ){//商户
				//获取商家信息
				$agent_info = db('info_agent')->where('id', $user_withdrawals_info['user_id'])->find();
				//计算当前累计提现
				$cut_total_withdrawals = bcadd($agent_info['total_withdrawals_money'], $user_withdrawals_info['balance'], 2);
				//计算累计提现次数
				$total_withdrawals_num = $agent_info['total_withdrawals_num'] + 1;
				//更新商家累计提现金额，次数
				db('info_agent')
						->where('id', $agent_info['id'])
						->update([
								'total_withdrawals_money'	=> $cut_total_withdrawals,
								'total_withdrawals_num'		=> $total_withdrawals_num
								]);
			}else{
				//获取用户信息信息
				$user_info = db('info_user')->where('id', $user_withdrawals_info['user_id'])->find();
				//计算当前累计提现
				$cut_total_withdrawals = bcadd($user_info['total_withdrawals_money'], $user_withdrawals_info['balance'], 2);
				//计算累计提现次数
				$total_withdrawals_num = $user_info['total_withdrawals_num'] + 1;
				//更新商家累计提现金额，次数
				db('info_user')
						->where('id', $user_info['id'])
						->update([
								'total_withdrawals_money'	=> $cut_total_withdrawals,
								'total_withdrawals_num'		=> $total_withdrawals_num
								]);
			}
			//获取总账号信息
			Db::table('sys_tongji')->where('id', 1)->setInc('tx_balances',$user_withdrawals_info['balance']);
			
		}else{	//驳回
			//提现记录状态
			$cut_withdrawals['cash_state'] = 3;
			//检查提现用户类型
			if($user_withdrawals_info['user_type'] == 1 ){//商户
				//获取商家信息
				$cut_agent_info = db('info_agent')
					->where('id', $user_withdrawals_info['user_id'])
					->find();
				//计算当前余额
				$cut_balance = bcadd($cut_agent_info['balance'], $user_withdrawals_info['balance'], 2);
				//更新商户余额信息
				Db::table('info_agent')->where('id', $user_withdrawals_info['user_id'])->update(['balance' => $cut_balance]);
				//新增商户余额变化日志
				agent_money_change($cut_agent_info['id'],0,0,0,10,$user_withdrawals_info['balance'],'余额提现失败退回',0);
				
			}else{	//会员
				//获取会员信息
				$cut_user_info = db('info_user')->where('id', $user_withdrawals_info['user_id'])->find();
				//计算当前余额
				$cut_balance = bcadd($cut_user_info['balance'], $user_withdrawals_info['balance'], 2);
				//更新会员余额信息
				Db::table('info_user')->where('id', $user_withdrawals_info['user_id'])->update(['balance' => $cut_balance]);
				//新增会员余额变化日志
				user_money_change($cut_user_info['id'],6,$user_withdrawals_info['balance'],'余额提现失败退回');
			}
		}
		//获取操作人
		$admin_info = db('tg_auth_user')->where('id',session('admin_id'))->find();
		
		$cut_withdrawals['remark'] 		= $content;
		$cut_withdrawals['thumb'] 		= $thumb;
		$cut_withdrawals['username'] 	= $admin_info['admin_name'];
		$cut_withdrawals['deal_time'] 	= now_time();
		//更新提现记录
		$result=Db::name('user_withdrawals')->where('id',$id)->update($cut_withdrawals);
		
       	if($result){
       		if($type==2){
       			$tem_w['title']='提现成功';
				$tem=db("sms_template")->where($tem_w)->find();
				$msg=send_sms($tem['sign_name'],$user_withdrawals_info['bank_mobile'],$tem['tid'],'balance',$user_withdrawals_info['balance']);
       		}elseif($type==3){
       			$tem_w['title']='提现失败';
				$tem=db("sms_template")->where($tem_w)->find();
				$msg=send_sms($tem['sign_name'],$user_withdrawals_info['bank_mobile'],$tem['tid'],'','');
       		}
			//发送短信
			return json(['code'=>200,'msg'=>'成功']);
       }else{
       		return json(['code'=>402,'msg'=>'失败']);
       }
    	
    	return $this->fetch();
    }
    
    //oem提现审核结果
	public function oem_tx_result(){
		$id			= input('id');			//提现记录id
		$type		= input('type');		//操作，2通过；3驳回	
		$thumb		= input('thumb');		//凭证截图
		$content	= input('content');		//内容
		
		//获取提现记录
		$oem_withdrawals_info = Db::name('oem_withdrawals')->where('id',input('id'))->find();
		
		if($type==2){	//审核通过
			$cut_withdrawals['cash_state'] = 2;
			
			//获取用户信息信息
			$cut_oem_info = db('info_oem')->where('id', $oem_withdrawals_info['oem_id'])->find();
			//计算当前累计提现
			$cut_total_withdrawals = bcadd($cut_oem_info['total_withdrawals_money'], $oem_withdrawals_info['balance'], 2);
			//计算累计提现次数
			$total_withdrawals_num = $cut_oem_info['total_withdrawals_num'] + 1;
			//更新商家累计提现金额，次数
			db('info_oem')
					->where('id', $cut_oem_info['id'])
					->update([
							'total_withdrawals_money'	=> $cut_total_withdrawals,
							'total_withdrawals_num'		=> $total_withdrawals_num
							]);
			//获取总账号信息
			Db::table('sys_tongji')->where('id', 1)->setInc('tx_balances',$oem_withdrawals_info['balance']);
		}else{	//驳回
			//提现记录状态
			$cut_withdrawals['cash_state'] = 3;
			//获取oem信息
			$cut_oem_info = db('info_oem')->where('id', $oem_withdrawals_info['oem_id'])->find();
			//计算当前余额
			$cut_balance = bcadd($cut_oem_info['balance'], $oem_withdrawals_info['balance'], 2);
			//更新商户余额信息
			Db::table('info_oem')->where('id', $cut_oem_info['id'])->update(['balance' => $cut_balance]);
			//新增商户余额变化日志
			balance_change(5,$oem_withdrawals_info['balance'],'余额提现失败退回',$cut_oem_info['id']);
				
		}
		//获取操作人
		$admin_info = db('tg_auth_user')->where('id',session('admin_id'))->find();
		
		$cut_withdrawals['remark'] 		= $content;
		$cut_withdrawals['thumb'] 		= $thumb;
		$cut_withdrawals['username'] 	= $admin_info['admin_name'];
		$cut_withdrawals['deal_time'] 	= now_time();
		//更新提现记录
		$result=Db::name('oem_withdrawals')->where('id',$id)->update($cut_withdrawals);
		
       	if($result){
       		if($type==2){
       			$tem_w['title']='提现成功';
				$tem=db("sms_template")->where($tem_w)->find();
				$msg=send_sms($tem['sign_name'],$oem_withdrawals_info['bank_mobile'],$tem['tid'],'balance',$oem_withdrawals_info['balance']);
       		}elseif($type==3){
       			$tem_w['title']='提现失败';
				$tem=db("sms_template")->where($tem_w)->find();
				$msg=send_sms($tem['sign_name'],$oem_withdrawals_info['bank_mobile'],$tem['tid'],'','');
       		}
			//发送短信
			return json(['code'=>200,'msg'=>'成功']);
       }else{
       		return json(['code'=>402,'msg'=>'失败']);
       }
    	
    	return $this->fetch();
    }

	//商家列表年审
    public function agent_limited()
    {
    	
		if(input('mobile') && input('mobile')>0){
			$where['a.mobile'] = ['like','%'.input('mobile').'%'];
		}
		
		if(input('tel') && input('tel')>0){
			$where['a.tel'] = ['like','%'.input('tel').'%'];
		}
		
		if(input('is_state') && input('is_state')>0){
			$where['a.is_state'] = input('is_state');
		}
		
		$shagent = Db::table('info_agent')->field("datediff(now(),effective_time) as jgtime,id")
		->where('datediff(now(),effective_time)<=10')
		->where('datediff(now(),effective_time)>0')->buildSql();
    	
    	$where['a.id'] = ['>',0];
		$where['a.status'] = 1;	
    	$list=Db::table('info_agent a')->
		join("info_oem b","a.agency_id2=b.id",'LEFT')->
		join("info_oem c","a.agency_id1=c.id",'LEFT')->
		join($shagent." d","a.id=d.id",'inner')		
    	->field("a.*,d.jgtime,b.user_name as pname,c.user_name as cname")
    	->order('add_time desc')
		->where($where)
    	->paginate(20,false,['query'=>request()->param()]);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
	
	//审核通过
	public function limited_pass(){
		$id=input('id');
		$where['effective_time']=now_time();
		$results=Db::name('info_agent')->where('id',$id)->update($where);

        if($results){
			return json(['code'=>200,'msg'=>'成功']);
       }else{
       		return json(['code'=>402,'msg'=>'失败']);
       }
    	
    	return $this->fetch();
    }
	
	//年审不通过冻结
	public function complain_frozen(){
		$data['status'] = 2;
	    $result=Db::table('info_agent')->where('id',$_GET['id'])->update($data);
        if($result){
			 return json(['code'=>200,'msg'=>'成功']);
       	}else{
       		return json(['code'=>402,'msg'=>'失败']);
       	}
		
		$data=Db::table('info_oem')->where('user_type=1 and user_level=3')->select();
    	$this->assign('data',$data);
    	return $this->fetch();
    }
    
	//银行签约
    public function agent_nh()
    {
    	$this->assign('id',input('id'));
    	$data=Db::table('info_agent')->find(input('id'));
    	$this->assign('data',$data);
		$qianyue=db("agent_qianyue")->where('agent_id',input('id'))->find();
    	if($_POST){
	        $parm['cb_miyao']		= trim($_POST['cb_miyao']);
	        $parm['cb_no']			= trim($_POST['cb_no']);
	        $parm['cb_clerk']		= trim($_POST['cb_clerk']);
	        $parm['cb_huilu']		= trim($_POST['cb_huilu']);
	        $parm['password']		= trim(md5_salt($_POST['password']));
	        $parm['cb_login_name']	= $_POST['cb_login_name'];
			$parm['is_state']	= 1;
			$parm['store_property']	= $qianyue['agent_type'];
	        $result=Db::table('info_agent')->where('id',input('id'))->update($parm);		
			
	        if($result){
	        	$id=input('id');
				//修改签约信息为1 终审通过
	        	$where['state']	= 1;
	        	$res=db("agent_qianyue")->where('agent_id',$id)->update($where);
				
	        	//修改用户  建立门店  修改关系				
				$agent=db("info_agent")->find($id);
				$update['is_agent']=2;
				$update['tj_agent']=$id;
				db('info_user')->where('id',$agent['user_id'])->update($update);
				$store_data['agent_id']=$id;
				$store_data['store_name']=$agent['agent_name'];
				$store_data['addres']=$agent['address'];
				$store_data['add_time']=now_time();
				db('agent_store')->insert($store_data);
				update_level($agent['user_id'],$id);
				
	        	//获取商家信息
        		$agent_info = Db::name('info_agent')->where('id',input('id'))->find();
        		//获取商家服务商信息
        		$city_info = Db::name('info_oem')->where('id',$agent_info['agency_id2'])->find();
				//短信开关 1 开  0 关
				if(config('sms_flag')==1){
		    		//发送短信提醒商家
		        	$tem_w['title']='审核成功';
					$tem=db("sms_template")->where($tem_w)->find();
					$msg=sendsms($tem['sign_name'],$agent_info['mobile'],$tem['tid'],'code','商家您的资质审核已通过',config('cut_oem_id'));
		        	
		        	//发送短信提醒服务商
		        	$tem_w['title']='审核成功';
					$tem=db("sms_template")->where($tem_w)->find();
					$msg=sendsms($tem['sign_name'],$city_info['mobile'],$tem['tid'],'code',$city_info['user_name']."：".$agent_info['mobile']."签约资料已审核通过",config('cut_oem_id'));
				}
				return json(['code'=>200,'msg'=>'成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'失败']);
	        }
        }
    	return $this->fetch();
    }

    //注册红包
    public function gold_reg()
    {
    	$list=Db::table('agent_activegold')->alias('a')
		->join('info_agent b','a.agent_id = b.id','LEFT')
		->field('a.id,a.user_num,a.gold_sum,a.type,a.hb_type,a.add_time,a.end_time,a.status,b.agent_name,b.mobile')
		->where('a.type = 1 and a.state = 1')
		->order('a.add_time desc')->paginate(15);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
    //扫码红包
    public function gold_sm()
    {
    	$list=Db::table('agent_activegold')->alias('a')
		->join('info_agent b','a.agent_id = b.id','LEFT')
		->field('a.id,a.user_num,a.gold_sum,a.type,a.hb_type,a.add_time,a.end_time,a.status,b.agent_name,b.mobile')
		->where('a.type = 3 and a.state = 1')
		->order('a.add_time desc')->paginate(15);
    	$this->assign('list',$list);
    	return $this->fetch();
    }	
	//分享红包
    public function gold_share()
    {
    	$list=Db::table('agent_activegold')->alias('a')
		->join('info_user b','a.user_id = b.id','LEFT')
		->field('a.id,a.user_num,a.gold_sum,a.type,a.hb_type,a.add_time,a.end_time,a.status,b.nickname as nick_name,b.mobile')
		->where('a.type = 2 and a.state = 1')
		->order('a.add_time desc')->paginate(15);
    	$this->assign('list',$list);
    	return $this->fetch();
    }
	
	//红包领取日志
	public function gold_log()
    {
    	$where['a.gold_id'] = input('gold_id');
		$list=db("agent_activelog")->alias('a')
		->join('info_user b','a.user_id = b.id','LEFT')
		->field('a.id,a.gold_id,a.user_id,a.gold_num,a.add_time,b.nickname as nick_name,b.mobile')
		->where($where)
		->order('a.add_time desc')->paginate(10);
		$this->assign('list',$list);
    	return $this->fetch();
    }
}
